// pages/together/index.js
let App = getApp();

Page({
	data: {
		words: '', 
		author_name: '',
		author_type: 0,
		wordTypeArray: ['篆书', '隶书', '楷书', '行书', '草书'],
		wordTypeIndex: 0,
		authorTypeArray: ['历代', '现代', '墨友'],
		authorTypeIndex: 0,
		authorNameArray: [],
		authorNameIndex: 0,
		formatArray: ['横排', '竖排'],
		formatIndex: 0,
		columnsArray: [2, 3, 4, 5, 6, 7, 8,],
		columnsIndex: 2,
		isQuery: false,
		rowsArray: [2, 3, 4, 5, 6, 7, 8,],
		rowsIndex: 0,
		matrixImg1: [],
		matrixImg2: [],
		imgList: []
	},

	wordsInput(e) {
		this.setData({words: e.detail.value});
	},

	// e.detail.value拿到的是字符串
	wordTypeChange(e) {
		this.setData({wordTypeIndex: parseInt(e.detail.value)}); 
		this.getNameList();
	},

	authorTypeChange(e) {
		this.setData({authorTypeIndex: parseInt(e.detail.value)});
		this.getNameList();
	},

	authorNameChange(e) {
		this.setData({authorNameIndex: parseInt(e.detail.value)});
	},

	columnsChange(e) {
		this.setData({columnsIndex: parseInt(e.detail.value)});
	},

	rowsChange(e) {
		this.setData({rowsIndex: parseInt(e.detail.value)});
	},

	formatChange(e) {
		this.setData({formatIndex: parseInt(e.detail.value)});
		// console.log(this.data.matrixImg)
	},

	getNameList() {
		let _this = this;

		if (_this.data.authorTypeIndex === 2) {
			App._getWithout('dic/collect/list/user', {type: _this.data.wordTypeIndex}, result => {
				_this.setData({authorNameArray: JSON.parse(result)})
			});
		} else {
			App._getWithout('dic/collect/list/call', {pic_type: _this.data.wordTypeIndex, author_type: _this.data.authorTypeIndex}, result => {
				_this.setData({authorNameArray: JSON.parse(result)})
			})
		}
		console.log(_this.data.authorNameArray);
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		// 初始化默认请求的作家名字是篆书，历代的
		let _this = this;
		App._getWithout('dic/collect/list/call', {pic_type: _this.data.wordTypeIndex, author_type: _this.data.authorTypeIndex}, result => {
			_this.setData({authorNameArray: JSON.parse(result)})
		});
	},

	submit() {
		let _this = this;

		_this.setData({
			imgList: [],
			matrixImg: []
		})

		// 这里输入的可能是长句含有一两个非法字符，所以非法的时候就不要else去清空了
		if (require("../../utils/util").verifyNotEmpty(_this.data.words)	&& require("../../utils/util").verifyChinese(_this.data.words)) {
			// 先开发为不用验证登录也可以使用集字功能
			App._getWithout('dic/collect/result', {code: wx.getStorageSync('openId'), words: _this.data.words, type: _this.data.wordTypeIndex, author_name: _this.data.authorNameArray[_this.data.authorNameIndex], author_type: _this.data.authorTypeIndex}, result => {
				result = JSON.parse(JSON.parse(result));
				
				let imgList = [];

				for (let item of result) {
					if (item['author_type'] === 0) {
						imgList.push({url: App.api_root + 'storage/dictionary/single/ancient' + item['img_url'],
													word: item['word']});
					} else if (item['author_type'] === 1) {
						imgList.push({url: App.api_root + 'storage/dictionary/single/modern' + item['img_url'],
													word: item['word']});
					} else if (item['author_type'] === 2) {
						imgList.push({url: App.api_root + 'storage/dictionary/single/user' + item['img_url'],
													word: item['word']});
					} else {
						imgList.push({url: App.api_root + item['img_url'],
													word: ''});
					}
				}
				_this.organizeImg(imgList);
				_this.setData({isQuery: true});
			})
		} 
	},

	formReset() {
		this.setData({words: '',
			wordTypeIndex: 0,
			authorTypeIndex: 0,
			authorNameIndex: 0,
			formatIndex: 0,
			columnsIndex: 2,
			isQuery: false,
			rowsIndex: 0,
			matrixImg1: [],
			matrixImg2: [],
			imgList: []
		})
	},

	organizeImg(imgList) {
		let columns = this.data.columnsArray[this.data.columnsIndex];  // 横排时候的每一行的数目
		let matrixImg1 = []  // 横排时候使用的图片地址列表
		let num = Math.ceil(imgList.length/columns)  // 横排时候的总行数
		for (let j=0; j<num; j++) {
			matrixImg1.push(imgList.slice(j*columns, (j+1)*columns))
			// console.log(imgList.slice(j*columns, (j+1)*columns))
		}
	
		let rows = this.data.rowsArray[this.data.rowsIndex];  // 竖排时候每一列的数目
		let blocks = Math.ceil(imgList.length/rows);  // 竖排时候总共的列数
		let matrixImg2 = [];  // 竖排时候使用的图片地址列表
		for (let i=0; i<rows; i++) {
			let tmp = [];
			for (let j=0; j<blocks; j++) {
				tmp.push(imgList[i+j*rows]);	
			}
			matrixImg2.push(tmp);
		}

		// console.log(matrixImg1, matrixImg2)

		this.setData({rows: rows, blocks: blocks, matrixImg1: matrixImg1, matrixImg2: matrixImg2, columns: columns, imgList: imgList});
		console.log(matrixImg1[0][0])
	},

	convert (e) {
		let word = e.currentTarget.dataset.word  // 通过事件拿取得值都是字符串
		// let test = e.currentTarget.dataset.test
		// console.log("test信息：", typeof test, test)
		let _this = this
		let horizon = parseInt(e.currentTarget.dataset.horizon)
		let vertical = parseInt(e.currentTarget.dataset.vertical)
		_this.setData({horizon, vertical})  // es6增强写法

		if (word !== undefined) {
			
			wx.navigateTo({
				url: '../convert/convert?word=' + word,
				events: {
					// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					// Opened: function(data) {
					// 	console.log(data)
					// }
				},
				success: function(res) {
					// 通过eventChannel向被打开页面传送数据
					res.eventChannel.emit('Opener', {word: word, wordTypeIndex: _this.data.wordTypeIndex, formatIndex: _this.data.formatIndex, horizon, vertical})
				}
			})
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function (options) {
	
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})